﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBearLite;
using LGCourt.Logic.Common;
using LGCourt.Logic.NBL;
using KMFrame.Util;
using KMFrame.Core;
using LGCourt.Logic.NBL.DBEntities;

namespace LGCourt.Logic.BLL
{
    public class UserInfo : LGCourtLogic
    {
        public bool CreateUser(pUserInfo user)
        {
            bool result = false;
            InsertSqlSection insert = NBLData.Insert(LGCourtColumns.pUserInfo)
                .AddColumn(LGCourtColumns.pUserInfo.UserName, user.UserName)
                .AddColumn(LGCourtColumns.pUserInfo.UserPWD, SecurityHelper.MD5(user.UserPWD, 32))
                .AddColumn(LGCourtColumns.pUserInfo.Sexuality, user.Sexuality)
                .AddColumn(LGCourtColumns.pUserInfo.IdentityType, user.IdentityType)
                .AddColumn(LGCourtColumns.pUserInfo.IdentityCard, user.IdentityCard)
                .AddColumn(LGCourtColumns.pUserInfo.Mobile, user.Mobile)
                .AddColumn(LGCourtColumns.pUserInfo.Phone, user.Phone)
                .AddColumn(LGCourtColumns.pUserInfo.Email, user.Email)
                .AddColumn(LGCourtColumns.pUserInfo.Address, user.Address)
                .AddColumn(LGCourtColumns.pUserInfo.UserGUID, Guid.NewGuid())
                .AddColumn(LGCourtColumns.pUserInfo.UserType, user.UserType)
                .AddColumn(LGCourtColumns.pUserInfo.Status, 0);
            int ri = insert.Execute();
            if (ri == 1)
                result = true;
            return result;
        }

        public bool CreateUser(string username, string password, string userkey, Byte UserType)
        {
            bool result = false;
            InsertSqlSection insert = NBLData.Insert(LGCourtColumns.pUserInfo)
                .AddColumn(LGCourtColumns.pUserInfo.UserName, username)
                .AddColumn(LGCourtColumns.pUserInfo.UserPWD, SecurityHelper.MD5(password, 32))
                .AddColumn(LGCourtColumns.pUserInfo.IdentityCard, userkey)
                .AddColumn(LGCourtColumns.pUserInfo.UserGUID, Guid.NewGuid())
                .AddColumn(LGCourtColumns.pUserInfo.UserType, UserType);
            int ri = insert.Execute();
            if (ri == 1)
                result = true;
            return result;
        }

        public bool ActiveUser(int identityType, string identityCard, string checkstr)
        {
            bool resutl = false;
            if(checkstr != "111111")
                return resutl;

            UpdateSqlSection update = NBLData.Update(LGCourtColumns.pUserInfo)
                .AddColumn(LGCourtColumns.pUserInfo.Status, 1)
                .Where(LGCourtColumns.pUserInfo.IdentityType == identityType
                && LGCourtColumns.pUserInfo.IdentityCard == identityCard);

            int ri = update.Execute();
            if (ri == 1)
                resutl = true;
            return resutl;
        }

        public int CheckActive(int identityType, string identityCard)
        {
            int resutl = -1;

            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                LGCourtColumns.pUserInfo.Status
            };

            PUserInfo userinfo = NBLData.Select(LGCourtColumns.pUserInfo, selectColumns)
                .Where(LGCourtColumns.pUserInfo.IdentityType == identityType
                && LGCourtColumns.pUserInfo.IdentityCard == identityCard)
                .ToSingleObject<PUserInfo>();

            if (userinfo != null)
                resutl = int.Parse(userinfo.Status.ToString());
            return resutl;
        }

        public bool GetPassword(int identityType, string identityCard, string checkstr, string password)
        {
            bool resutl = false;
            if (checkstr != "111111")
                return resutl;

            UpdateSqlSection update = NBLData.Update(LGCourtColumns.pUserInfo)
                .AddColumn(LGCourtColumns.pUserInfo.UserPWD, SecurityHelper.MD5(password, 32))
                .Where(LGCourtColumns.pUserInfo.IdentityType == identityType && LGCourtColumns.pUserInfo.IdentityCard == identityCard);
            
            int ri = update.Execute();
            if (ri == 1)
                resutl = true;
            return resutl;
        }

        public bool ChangePassword(UserPassword password)
        {
            bool resutl = false;
            UpdateSqlSection update = NBLData.Update(LGCourtColumns.pUserInfo)
                .AddColumn(LGCourtColumns.pUserInfo.UserPWD, SecurityHelper.MD5(password.NewPassWord, 32))
                .Where(LGCourtColumns.pUserInfo.UserID == password.UserID && LGCourtColumns.pUserInfo.UserPWD == SecurityHelper.MD5(password.OldPassWord, 32));

            int ri = update.Execute();
            if (ri == 1)
                resutl = true;
            return resutl;
        }

        /// <summary>
        /// 创建记者之家用户
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="userkey"></param>
        /// <param name="UserType"></param>
        /// <returns></returns>
        public bool CreateReporterUser(string username, string password, string userkey,string phone,string email, Byte UserType)
        {
            bool result = false;
            InsertSqlSection insert = NBLData.Insert(LGCourtColumns.pUserInfo)
                .AddColumn(LGCourtColumns.pUserInfo.UserName, username)
                .AddColumn(LGCourtColumns.pUserInfo.UserPWD, SecurityHelper.MD5(password, 32))
                .AddColumn(LGCourtColumns.pUserInfo.IdentityCard, userkey)
                .AddColumn(LGCourtColumns.pUserInfo.Phone, phone)
                .AddColumn(LGCourtColumns.pUserInfo.Email, email)
                .AddColumn(LGCourtColumns.pUserInfo.UserGUID, Guid.NewGuid())
                .AddColumn(LGCourtColumns.pUserInfo.UserType, UserType);
            int ri = insert.Execute();
            if (ri == 1)
                result = true;
            return result;
        }

        public PUserInfo Login(string identityCard, string password, int usertype)
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                LGCourtColumns.pUserInfo.UserID, 
                LGCourtColumns.pUserInfo.UserName,
                LGCourtColumns.pUserInfo.UserGUID,
                LGCourtColumns.pUserInfo.UserType
            };
            return NBLData.Select(LGCourtColumns.pUserInfo, selectColumns)
                .Where(LGCourtColumns.pUserInfo.IdentityCard == identityCard && LGCourtColumns.pUserInfo.UserPWD == SecurityHelper.MD5(password, 32)
                    && LGCourtColumns.pUserInfo.UserType == usertype)
                .ToSingleObject<PUserInfo>();
        }

        /// <summary>
        /// 删除记者之家用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool DelReporterUser(int userID)
        {
            bool result = false;
            DeleteSqlSection deleteUserInfo = NBLData.Delete(LGCourtColumns.pUserInfo)
                 .Where(LGCourtColumns.pUserInfo.UserID == userID);
            int ri = deleteUserInfo.Execute();
            if (ri > 0)
                result = true;
            return result;
        }

        /// <summary>
        /// 获取记者之家用户列表信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public PageResult GetUserInfoList(PageParam condition, string keyword)
        {
            PageResult result = new PageResult();

            WhereClip where = LGCourtColumns.pUserInfo.UserType == UserTypes.REPORTS;
            if (!string.IsNullOrEmpty(keyword))
            {
                LGCourtColumns.pUserInfo.UserName.Like("%" + keyword + "%");
            }
            result.ResultList = NBLData
                .Select(LGCourtColumns.pUserInfo)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.pUserInfo.UserID)
                .OrderBy(LGCourtColumns.pUserInfo.UserID.Desc)
                .ToList<pUserInfo>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.pUserInfo, LGCourtColumns.pUserInfo.UserID, null);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        /// <summary>
        /// 获取单点登陆名称
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public pUserLinkInfo GetUserLinkInfo(pUserLinkInfo param)
        {
            return NBLData.Select(LGCourtColumns.pUserLinkInfo, null)
                .Where(LGCourtColumns.pUserLinkInfo.UserName == param.UserName)
                .ToSingleObject<pUserLinkInfo>();
        }

    }

    public class PUserInfo
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public System.Guid UserGUID { get; set; }
        public Byte? UserType { get; set; }
        public Byte? Status { get; set; }
    }

    public class UserPassword
    {
        public int UserID { get; set; }
        public string OldPassWord { get; set; }
        public string NewPassWord { get; set; }
    }

    /// <summary>
    /// 用户类型
    /// </summary>
    public static class UserTypes
    {
        /// <summary>
        /// 普通帐户
        /// </summary>
        public static Byte GENERAL
        {
            get
            {
                return 1;
            }
        }

        /// <summary>
        /// 记者之家用户
        /// </summary>
        public static Byte REPORTS
        {
            get
            {
                return 2;
            }
    }
    }
}
